<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>1.点击按钮改变值</title>
    <script src="js/vue.js"></script>
</head>

<body>
    <div id="app">
        <custom-count :count="count"></custom-count>
    </div>

    <script>
        Vue.component('custom-count', {
            props: ["count"],
            data: function() {
                return {
                    num: this.count
                }
            },
            template: `
                        <div>
                            <h2>我是一个自定义组件</h2>
                            <div>
                                <span>数量：</span>
                                <button @click="changeMinus">-</button>
                                <span>{{num}}</span>
                                <button @click="changePlus">+</button>
                                
                            </div>
                            
                        </div>
                        `,
            methods: {
                changePlus() {
                    this.num++
                },
                changeMinus() {
                    if (this.num > 0) {
                        this.num--
                    } else {
                        alert("购买数量必须大于0")
                    }

                }

            }

        })

        new Vue({
            el: "#app",
            data: {
                count: 0
            }
        })
    </script>
</body>

</html>